<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/11/04 20:30:24 $)
// $RCSfile: functions_pdaui.php,v $ : $Revision: 1.3 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


/**
* Renders the Select Box
*/
function renderTicketSelect()
{
	global $_SWIFT, $template;

	$departmentidlist = getAssignedDepartments(MODULE_TICKETS);

	$alldeptitle = $_SWIFT["language"]["alldepartments"];

	$optiondata = '<option value="0"'. iif(empty($_REQUEST["departmentid"]), ' selected') .'>'. $alldeptitle .'</option>';
	foreach ($_SWIFT["departmentcache"] as $key=>$val)
	{
		if ($val["departmentmodule"] != MODULE_TICKETS || !in_array($val["departmentid"], $departmentidlist))
		{
			continue;
		}

		$ticketcount = "(".$_SWIFT["ticketcountcache"][$val["departmentid"]]["totaltickets"].")";
		if ($ticketcount == "()")
		{
			$ticketcount = "(0)";
		}
		unset($ticketcount);

		$isdepnew = false;
		if ($_SWIFT["ticketcountcache"][$val["departmentid"]]["lastactivity"] > $_SWIFT["staff"]["lastvisit"])
		{
			$isdepnew = true;
		}

		$isdepselected = false;
		if ($_REQUEST["departmentid"] == $val["departmentid"] && empty($_REQUEST["ticketstatusid"]))
		{
			$isdepselected = true;
		}

		unset($indent); unset($gdepselect);
		if ($_REQUEST["departmentid"] == $val["departmentid"])
		{
			$gdepselect = " &gt; ";
			$indent = "&nbsp;&nbsp;&nbsp;";
		}

		$optiondata .= '<option value="d_'. intval($val["departmentid"]) .'"'. iif($isdepnew, ' style="BACKGROUND-COLOR: #EDB1A3;"'). iif($isdepselected, " selected") .'>'. $gdepselect .htmlspecialchars($val["title"]) .'&nbsp;&nbsp;&nbsp;</option>';

		foreach ($_SWIFT["statuscache"] as $statuskey=>$statusval)
		{
			if ($statusval["departmentid"] != 0 && $statusval["departmentid"] != $val["departmentid"])
			{
				continue;
			}

			$isstatusnew = false;
			if ($_SWIFT["ticketcountcache"][$val["departmentid"]][$statusval["ticketstatusid"]]["lastactivity"] > $_SWIFT["staff"]["lastvisit"])
			{
				$isstatusnew = true;
			}

			$statuscount = "(".number_format($_SWIFT["ticketcountcache"][$val["departmentid"]][$statusval["ticketstatusid"]]["count"],0).")";
			if ($statuscount == "(0)" || $statusval["displaycount"] != 1)
			{
				unset($statuscount);
			}

			$isstatusselected = false;
			if ($_REQUEST["ticketstatusid"] == $statusval["ticketstatusid"] && $_REQUEST["departmentid"] == $val["departmentid"])
			{
				$isstatusselected = true;
			}

			$optiondata .= '<option value="t_'.intval($val["departmentid"]).':'. intval($statusval["ticketstatusid"]) .'"'. iif($isstatusnew,' style="BACKGROUND-COLOR: #EDB1A3;"'). iif($isstatusselected, " selected") .'>&nbsp;&nbsp;&nbsp;'. $indent. iif($isstatusselected," &gt; ") .htmlspecialchars($statusval["title"]) .'&nbsp;&nbsp;&nbsp;</option>';
		}
	}

	return $optiondata;
}

/**
* Render the PDA ticket grid
*/
function renderPDATicketGrid($ticketviewid, $departmentidlist, $ticketstatusidlist, $ticketsearchid = false)
{
	global $dbCore, $_SWIFT, $datastore, $_fieldpointer, $template;

	ob_start();

	$_ticketview = &$_SWIFT["ticketviewcache"][$ticketviewid];
	if (!is_array($_ticketview))
	{
		trigger_error($_SWIFT["language"]["invalidview"], E_USER_ERROR);
	}

	$appendurl = "&departmentid=". intval($_REQUEST["departmentid"]) ."&ticketstatusid=". intval($_REQUEST["ticketstatusid"]) ."&ticketsearchid=".intval($_REQUEST["ticketsearchid"]);
	$_grid = $_update = array();
	$_gridset = &$_SWIFT["ticketgridcache"][$_SWIFT["staff"]["staffid"]];
	if (!is_array($_gridset))
	{
		$_gridset = array();
		$gridsetupdate = true;
	}

	$_staffid = &$_SWIFT["staff"]["staffid"];

	$_limit = $_ticketview["ticketsperpage"];
	if (!$_limit)
	{
		$_limit = "15";
	}
	$_grid[TICKETGRID_LIMIT] = intval($_limit);

	// Get the current offset for this page
	if (!empty($_REQUEST["offset"]) && is_numeric($_REQUEST["offset"]))
	{
		$_offset = intval($_REQUEST["offset"]);
	} else {
		$_offset = "0";
	}

	// Is sort order changed or is it default one? First we see if there is a custom sort order passed by user
	// just use the default sort order
	$_grid[TICKETGRID_SORTBY] = $_fieldpointer[$_ticketview["sortby"]]["name"];
	if (empty($_grid[TICKETGRID_SORTBY]))
	{
		$_grid[TICKETGRID_SORTBY] = $_fieldpointer[FIELD_LASTACTIVITY]["name"];
		$_update["sortby"] = FIELD_LASTACTIVITY;
	}
	$_grid[TICKETGRID_SORTORDER] = iif($_ticketview["sortorder"]==VIEW_ASC, "ASC", "DESC");

	// Build sort statement
	$sortstatement = iif($_grid[TICKETGRID_SORTBY], " ORDER BY ". $dbCore->escape($_grid[TICKETGRID_SORTBY]) .iif(strtolower($_grid[TICKETGRID_SORTORDER])=="desc", " DESC", " ASC"));

	$fields = $_customfieldidlist = array();
	$_findex = 0;

	/**
	* ###############################################
	* BUILD THE QUERIES
	* ###############################################
	*/
	$_sortjoin = $sortstatement." LIMIT ". intval($_offset) .", ". intval($_limit) .";";
	$ticketcount = 0;
	$_ticketidlist = array();
	$strdepartmentid = buildIN($departmentidlist);
	$strticketstatus = buildIN($ticketstatusidlist);
	$ojoinsql = array();
	unset($ownerjoin);
	if ($_ticketview["viewalltickets"] == 1)
	{
		// No Owner Join
	} else {
		if ($_ticketview["viewunassigned"] == 1) {
			$ojoinsql[] = "0";
		}
		if ($_ticketview["viewassigned"] == 1)
		{
			$ojoinsql[] = $_SWIFT["staff"]["staffid"];
		}
	}
	
	if (count($ojoinsql))
	{
		$ownerjoin = " AND `ownerstaffid` IN (". buildIN($ojoinsql) .")";
	}

	// ======= TICKET SEARCH TEST =======
	$usesearch = false;
	if ($ticketsearchid)
	{
		// Try to fetch the search contents
		$_search = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."ticketsearches` WHERE `ticketsearchid` = '". intval($ticketsearchid) ."';");
		if (!empty($_search["ticketsearchid"]) && $_search["staffid"] == $_SWIFT["staff"]["staffid"])
		{
			$searchticketidlist = unserialize($_search["ticketidlist"]);
			if (_is_array($searchticketidlist))
			{
				$usesearch = true;
				$ticketcount = count($searchticketidlist);
			}
		}
	}
	if (!$usesearch)
	{
		unset($ticketsearchid);
	}
	if (!$usesearch)
	{
		// First get the total tickets that fall under our criteria.
		$_ticketcnt = $dbCore->queryFetch("SELECT COUNT(*) AS totalitems FROM `". TABLE_PREFIX ."tickets` AS t WHERE t.departmentid IN (". $strdepartmentid .") AND t.ticketstatusid IN (". $strticketstatus .")". $ownerjoin .";");
		if (empty($_ticketcnt["totalitems"]))
		{
			$ticketcount = 0;
		} else {
			$ticketcount = intval($_ticketcnt["totalitems"]);
		}
	}

	// Lets first get the total
	$_grid[TICKETGRID_TOTALITEMS] = $ticketcount;

	$name = "ticketlist";

	printFormStart($name, "index.php", "POST", false, "", "");


	$gridurl = buildURL("index.php?_m=". $_SWIFT["currentmodule"] ."&_a=". $_SWIFT["currentaction"].$appendurl);
	// ======= START PAGINATION TABLE =======

	$pagination = pagination($gridurl, $_grid[TICKETGRID_TOTALITEMS], $_limit, $_offset, "5", "pageoftotal", TRUE, FALSE, TRUE);
	echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">'.SWIFT_CRLF;
	echo '<tr><td><img src="'. $_SWIFT["themepath"] .'space.gif" width="1" height="6" /></tr></td>'.SWIFT_CRLF;
	echo '<tr><td><table width="100%"  border="0" cellspacing="0" cellpadding="0"><tr valign="top"><td align="left"><table border="0" cellpadding="0" cellspacing="1" class="tborder"><tr>'.$pagination.'</tr></table></td></tr></table></tr></td>'.SWIFT_CRLF;
	echo '<tr><td><img src="'. $_SWIFT["themepath"] .'space.gif" width="1" height="6" /></tr></td>'.SWIFT_CRLF;
	echo '</table>';
	// ======= END PAGINATION TABLE =======

	echo '<table width="100%" border="0" cellspacing="1" cellpadding="2" class="tborder">'.SWIFT_CRLF;


	$totalfields = count($fields)+1;
	$colspan = $totalfields;

	// ======= START GRID HEADER =======

	// ======= START GRID CONTENTS =======
	// Fetch the required data now
	if ($usesearch == true)
	{
		$_ticketidlist = &$searchticketidlist;
	} else {
		$dbCore->query("SELECT t.ticketid FROM `". TABLE_PREFIX ."tickets` AS t WHERE t.departmentid IN (". $strdepartmentid .") AND t.ticketstatusid IN (". $strticketstatus .")". $ownerjoin ." ". $_sortjoin .";");
		while ($dbCore->nextRecord())
		{
			$_ticketidlist[] = $dbCore->Record["ticketid"];
		}
	}

	$strticketid = buildIN($_ticketidlist);
	$_customfieldvalues = array();
	if (count($_customfieldidlist))
	{
		$dbCore->query("SELECT `customfieldid`, `typeid`, `fieldvalue` FROM `". TABLE_PREFIX ."customfieldvalues` WHERE `customfieldid` IN (". buildIN($_customfieldidlist) .") AND `typeid` IN (". $strticketid .");");
		while ($dbCore->nextRecord())
		{
			$_customfieldvalues[$dbCore->Record["typeid"]][$dbCore->Record["customfieldid"]] = $dbCore->Record["fieldvalue"];
		}
	}

	if ($_SWIFT["settings"]["t_tpreview"] == 1)
	{
		$_tquery = "SELECT t.*, tp.contents AS preview FROM `". TABLE_PREFIX ."tickets` AS t LEFT JOIN `". TABLE_PREFIX ."ticketposts` AS tp ON (tp.ticketpostid = t.lastpostid) WHERE t.ticketid IN (". $strticketid .") ". iif($usesearch==true, $_sortjoin, $sortstatement).";";
	} else {
		$_tquery = "SELECT t.* FROM `". TABLE_PREFIX ."tickets` AS t WHERE t.ticketid IN (". $strticketid .") ". iif($usesearch==true, $_sortjoin, $sortstatement).";";
	}

	$flagbgcolors = array(FLAG_PURPLE => "#A5587C", FLAG_ORANGE => "#FF8C5A", FLAG_GREEN => "#8BB467", FLAG_YELLOW => "#FFC160", FLAG_RED => "#CF5D60", FLAG_BLUE => "#5C83B4");
	$flagicons = array(FLAG_PURPLE => "icon_purpleflag.gif", FLAG_ORANGE => "icon_orangeflag.gif", FLAG_GREEN => "icon_greenflag.gif", FLAG_YELLOW => "icon_yellowflag.gif", FLAG_RED => "icon_redflag.gif", FLAG_BLUE => "icon_blueflag.gif");

	$dbCore->query($_tquery, 2);
	$cnt = 0;
	while ($dbCore->nextRecord(2))
	{
		unset($record);
		unset($bgrecord);
		$cnt++;
		$rowbg = getRowBG();
		$id = $dbCore->Record2["ticketid"];
		$record = $dbCore->Record2;

		if (!empty($_SWIFT["prioritycache"][$record["priorityid"]]["bgcolorcode"]))
		{
			$trbgcolor = $_SWIFT["prioritycache"][$record["priorityid"]]["bgcolorcode"];
		} else {
			unset($trbgcolor);
		}

		if ($record["hasnotes"] == 1)
		{
			$rowbg = "rownotes";
		}
		$record["overdue"] = false;
		if ($record["duetime"] != 0)
		{
			if (DATENOW >= $record["duetime"])
			{
				// Overdue
				$record["t.duetime"] = '<span class=\'tickettextred\'>'.$_SWIFT["language"]["overdue"].'</span>';
				$record["overdue"] = true;
			} else {
				$record["t.duetime"] = strColorDate(($record["duetime"]-DATENOW), true);
			}
		}

		if ($record["overdue"] == true)
		{
			$trbgcolor = $_SWIFT["settings"]["t_overduecolor"];
		}

		$record["t.ticketid"] = "<a href='". $ticketurl ."' target='". iif($_gridset["opennewwindow"]==1, "_blank", "_top") ."'>".iif($_SWIFT["settings"]["t_eticketid"]=="seq", intval($record["ticketid"]), $record["ticketmaskid"]).'</a>';

		// We replace all instances of field names in [] braces with their respective values, *much* faster (trust me) than doing a for loop for fields inside another while loop ;)
		$ticketurl = "index.php?_m=tickets&_a=viewticket&ticketid=". intval($record["ticketid"]).iif(!empty($_search["keywords"]), "&highlight=".urlencode($_search["keywords"])) .iif(!empty($_REQUEST["departmentid"]), "&departmentid=". intval($_REQUEST["departmentid"])) .iif(!empty($_REQUEST["ticketstatusid"]), "&ticketstatusid=". intval($_REQUEST["ticketstatusid"])) .iif(!empty($_REQUEST["ticketsearchid"]), "&ticketsearchid=".intval($_REQUEST["ticketsearchid"]));
		$record["t.subject"] = '<a href=\''. $ticketurl .'\'>'. iif($record["hasattachments"]==1,'&nbsp;<img src=\''. $_SWIFT["themepath"]. 'icon_paperclip.gif\' border=\'0\' align=\'absmiddle\' />') .'&nbsp;'.htmlspecialchars($record["subject"]).'</a>'.iif($_SWIFT["settings"]["t_enpagin"]==1, smallPagination("?_m=tickets&_a=viewticket&ticketid=".$record["ticketid"], ($record["totalreplies"]+1), intval($_SWIFT["settings"]["t_postlimit"]), -1, TRUE));
		$record["t.emailqueueid"] = htmlspecialchars(stripName($_SWIFT["queuecache"][$record["emailqueueid"]]["email"], 15));
		$record["t.departmentid"] = htmlspecialchars(stripName($_SWIFT["departmentcache"][$record["departmentid"]]["title"], 13));
		$record["t.ticketstatusid"] = '<font color=\''. $_SWIFT["statuscache"][$record["ticketstatusid"]]["statuscolor"] .'\'>'.htmlspecialchars($_SWIFT["statuscache"][$record["ticketstatusid"]]["title"]).'</font>';
		$record["t.dateline"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $record["dateline"]);
		$record["t.ownerstaffid"] = iif(!empty($record["ownerstaffid"]), htmlspecialchars(stripName($_SWIFT["staffcache"][$record["ownerstaffid"]]["fullname"], 15)), $_SWIFT["language"]["unassigned"]);
		$record["t.priorityid"] = '<font color=\''. $_SWIFT["prioritycache"][$record["priorityid"]]["frcolorcode"] .'\'>'.htmlspecialchars($_SWIFT["prioritycache"][$record["priorityid"]]["title"]).'</font>';
		$record["t.lastreplier"] = htmlspecialchars(stripName(iif(empty($record["lastreplier"]), $record["fullname"], $record["lastreplier"]), 15));
		$record["t.fullname"] = htmlspecialchars(stripName($record["fullname"], 15));
		$record["t.timeworked"] = intval($record["timeworked"]);
		$record["t.email"] = htmlspecialchars(stripName($record["email"], 20));
		$record["t.totalreplies"] = intval($record["totalreplies"]);
		$record["t.lastactivity"] = strColorDate((DATENOW-$record["lastactivity"]));
		$record["t.assignstatus"] = iif(!empty($record["ownerstaffid"]), '<img src=\''. $_SWIFT["themepath"] .'icon_greenarrow.gif\' border=\'0\' align=\'absmiddle\' />&nbsp;'.$_SWIFT["language"]["tassigned"], '<img src=\''. $_SWIFT["themepath"] .'icon_grayarrow.gif\' border=\'0\' align=\'absmiddle\' />&nbsp;'.$_SWIFT["language"]["tunassigned"]);

		if ($record["lastactivity"] > $_SWIFT["staff"]["lastvisit"])
		{
			// New Ticket
			$record["t.dateicon"] = '<img src=\''. $_SWIFT["themepath"]. iif($record["isphonecall"]=="1", "icon_ticketphone.gif", 'icon_ticketyellow.gif') .'\' border="0" />';
			$bgrecord["t.dateicon"] = '#FFF8E9';
		} else {
			// Normal Ticket
			$record["t.dateicon"] = '<img src=\''. $_SWIFT["themepath"]. iif($record["isphonecall"]=="1", "icon_ticketphone.gif", 'icon_ticket.gif') .'\' border="0" />';
		}

		$record["t.flagtype"] = iif(!empty($record["flagtype"]), '<img src=\''. $_SWIFT["themepath"]. $flagicons[$record["flagtype"]] .'\' border=\'0\' />', '<img src=\''. $_SWIFT["themepath"] .'icon_flagblank.gif\' width=\'18\' height=\'20\' border=\'0\' />');

		if ($record["hasnotes"] == 1 && $record["hasbilling"] == 1)
		{
			$noteimage = "icon_ticketnotebilling.gif";
		} else if ($record["hasnotes"] == 1) {
			$noteimage = "icon_ticketnote.gif";
		} else if ($record["hasbilling"] == 1) {
			$noteimage = "icon_timetrack.gif";
		} else {
			$noteimage = "space.gif";
		}

		$record["t.hasnotes"] ='<img src=\''. $_SWIFT["themepath"]. $noteimage.'\' border=\'0\' />';

		$bgrecord["t.flagtype"] = $flagbgcolors[$record["flagtype"]];

		$tooltip = $_SWIFT["language"]["f_ticketid"].": ".$record["t.ticketid"]."<BR />". $_SWIFT["language"]["f_subject"].": ".htmlspecialchars($record["subject"])."<BR />". $_SWIFT["language"]["f_queue"].": ".$record["t.emailqueueid"]."<BR />". $_SWIFT["language"]["f_department"].": ".$record["t.departmentid"]."<BR />". $_SWIFT["language"]["f_ticketstatus"].": ".$record["t.ticketstatusid"]."<BR />". $_SWIFT["language"]["f_date"].": ".$record["t.dateline"]."<BR />". $_SWIFT["language"]["f_owner"].": ".$record["t.ownerstaffid"]."<BR />". $_SWIFT["language"]["f_priority"].": ".$record["t.priorityid"]."<BR />". $_SWIFT["language"]["f_lastreplier"].": ".htmlspecialchars($record["lastreplier"])."<BR />". $_SWIFT["language"]["f_fullname"].": ".htmlspecialchars($record["fullname"])."<BR />". $_SWIFT["language"]["f_timeworked"].": ".$record["t.timeworked"]."<BR />". $_SWIFT["language"]["f_email"].": ".htmlspecialchars($record["email"])."<BR />". $_SWIFT["language"]["f_totalreplies"].": ".$record["t.totalreplies"]."<BR />". $_SWIFT["language"]["f_lastactivity"].": ".$record["t.lastactivity"]."<BR />". $_SWIFT["language"]["f_duedate"].": ".$record["t.duetime"]."<BR />".iif(!empty($record["preview"]), "<HR />".htmlspecialchars(preg_replace("#(\r\n|\r|\n)#s", "<BR />", $record["preview"])));

		$record["t.dateicon"] = '<a id="ldateicon'. intval($record["ticketid"]) .'" href="index.php?_m=tickets&_a=viewticket&ticketid='. intval($record["ticketid"]) .'" onmouseover="javascript:CustomOver(\''. addslashes($tooltip) .'\', \'ldateicon'. intval($record["ticketid"]) .'\', \'dateicon'. intval($record["ticketid"]) .'\');" onmouseout="return nd();">'.  $record["t.dateicon"].'</a>';

		echo '<tr id="trid'. $record["ticketid"] .'" style="BACKGROUND-COLOR:'. $trbgcolor .';" class="'. $rowbg .'">'.SWIFT_CRLF;
		echo '<td align="left" valign="middle" width="1">'. "<input type='checkbox' name='itemid[]' value='". intval($record["ticketid"]) ."'>" .'</td>'.SWIFT_CRLF;
		echo '<td align="left" valign="middle">'. '<span class="smalltext">'. $record["t.lastreplier"] .'</span></td>'.SWIFT_CRLF;
		echo '<td align="left" valign="middle">'. '<span class="smalltext">'. $record["t.lastactivity"] .'</span></td>'.SWIFT_CRLF;
		echo '<td align="left" valign="middle">'. '<span class="smalltext">'. $record["t.duetime"] .'</span></td>'.SWIFT_CRLF;
		echo '<tr id="trid'. $record["ticketid"] .'" style="BACKGROUND-COLOR:'. $trbgcolor .';" class="'. $rowbg .'">'.SWIFT_CRLF;
		echo '<td align="left" valign="middle" colspan="4"><span class="tabletitle">'. $record["t.subject"] .'</span></td>'.SWIFT_CRLF;

		echo '</tr>'.SWIFT_CRLF;
	}
	printMainTableFooter();

	// ======= MASS ACTION STUFF =======
	// Ticket Status Options
	$statusoptions = $priorityoptions = $departmentoptions = "";
	foreach ($_SWIFT["statuscache"] as $key=>$val)
	{
		$statusoptions .= '<option value="'. $val["ticketstatusid"] .'" style="color:'. $val["statuscolor"] .'">'. htmlspecialchars($val["title"]) .'</option>';
	}
	
	// Ticket Priorities
	foreach ($_SWIFT["prioritycache"] as $key=>$val)
	{
		$priorityoptions .= '<option value="'. $val["priorityid"] .'" style="background:'. $val["bgcolorcode"] .';color:'. $val["frcolorcode"] .'">'. htmlspecialchars($val["title"]) .'</option>';
	}
	
	// Departments
	foreach ($_SWIFT["departmentcache"] as $key=>$val)
	{
		if ($val["departmentmodule"] == MODULE_TICKETS)
		{
			$departmentoptions .= '<option value="'. $val["departmentid"] .'">'. htmlspecialchars($val["title"]) .'</option>';
		}
	}
	
	// Ticket Owners
	$owneroptions .= '<option value="0">'. $_SWIFT["language"]["unassigned"] .'</option>';
	foreach ($_SWIFT["staffcache"] as $key=>$val)
	{
		$owneroptions .= '<option value="'. $val["staffid"] .'">'. htmlspecialchars($val["fullname"]) .'</option>';
	}

	// ======= START SECOND PAGINATION TABLE =======
	echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">'.SWIFT_CRLF;
	echo '<tr><td><img src="'. $_SWIFT["themepath"] .'space.gif" width="1" height="6" /></tr></td>'.SWIFT_CRLF;
	echo '<tr><td><table width="100%"  border="0" cellspacing="0" cellpadding="0"><tr valign="top"><td align="left"><table border="0" cellpadding="0" cellspacing="1" class="tborder"><tr>'.$pagination.'</tr></table></td></tr></table></tr></td>'.SWIFT_CRLF;
	echo '<tr><td><img src="'. $_SWIFT["themepath"] .'space.gif" width="1" height="6" /></tr></td>'.SWIFT_CRLF;
	echo '</table>';
	// ======= END SECOND PAGINATION TABLE =======


	echo '<table width="100%" border="0" cellspacing="1" cellpadding="3" class="tborder">'.SWIFT_CRLF;
	echo '<tr id="trmassaction">'.SWIFT_CRLF;
	echo '<td class="'. getRowBG() .'" align="center" valign="middle" colspan="'. $colspan .'" nowrap>
	<select name="ma_options" class="gridmassaction">
	<option value="0" selected>'. $_SWIFT["language"]["maoptions"] .'</option>
	<option value="delete">'. $_SWIFT["language"]["delete"] .'</option>
	<option value="bandelete">'. $_SWIFT["language"]["bandelete"] .'</option>
	<option value="mergetickets">'. $_SWIFT["language"]["mergetickets"] .'</option>
	<option value="markasdue">'. $_SWIFT["language"]["markasdue"] .'</option>
	<option value="markduein24">'. $_SWIFT["language"]["markduein24hrs"] .'</option>
	</select> + 
	<select name="ma_staffid" class="gridmassaction">
	<option value="" selected>'. $_SWIFT["language"]["maowner"] .'</option>
	'. $owneroptions .'
	</select> +
	<select name="ma_departmentid" class="gridmassaction">
	<option value="0" selected>'. $_SWIFT["language"]["madepartments"] .'</option>
	'. $departmentoptions .'
	</select> + 
	<select name="ma_ticketstatusid" class="gridmassaction">
	<option value="0" selected>'. $_SWIFT["language"]["maticketstatus"] .'</option>
	'. $statusoptions .'
	</select>
	<input type="submit" name="submitbtn" value="'. $_SWIFT["language"]["go"] .'" class="swiftmabutton">
	</td>'.SWIFT_CRLF;
	echo '</tr>';
	echo '</table>'.SWIFT_CRLF;
	// ======= END GRID CONTENTS =======

	addHiddenField("_m", "tickets");
	addHiddenField("_a", "manage");
	addHiddenField("departmentid", intval($_REQUEST["departmentid"]));
	addHiddenField("ticketstatusid", intval($_REQUEST["ticketstatusid"]));
	addHiddenField("ticketsearchid", intval($_REQUEST["ticketsearchid"]));
	printFormEnd();

	$gridoutput = ob_get_contents();
	ob_end_clean();

	echo $gridoutput;
}

?>